package cn.wen.eeayjoke.ui.home;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import cn.wen.base.util.DensityUtils;
import cn.wen.base.view.CompatViewPager;
import cn.wen.base.view.limit.BottomLayout;
import cn.wen.base.view.limit.TabAdapter;
import cn.wen.core.titlebar.DefTitleBar;
import cn.wen.core.titlebar.TitleBarUtil;
import cn.wen.core.ui.AppActivity;
import cn.wen.eeayjoke.R;
import cn.wen.eeayjoke.adapter.home.HomeFmtPagerAdapter;

/**
 * APP主页面
 */

public class HomeActivity extends AppActivity {
    @BindView(R.id.view_pager)
    CompatViewPager mViewPager;
    @BindView(R.id.bottom_layout)
    BottomLayout mBottomLayout;

    /**
     * 底部的标题
     */
    private String[] mTabTitles;
    /**
     * 底部的图标
     */
    private Integer[] mTabIcons = {R.drawable.endtab1_selector, R.drawable.endtab2_selector,
            R.drawable.endtab3_selector, R.drawable.endtab4_selector, R.drawable.entab5_selector};


    @Override
    protected Object getContentLayout() {
        return R.layout.activity_bottom;
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        TitleBarUtil.setStatusBarColor(this);
        setSwipeBackEnable(false);
        // 底部的标题
        mTabTitles = getResources().getStringArray(R.array.tab_array);

    }

    @Override
    protected void initView(View contentView) {
        mViewPager.setOffscreenPageLimit(4);
        mViewPager.setAdapter(new HomeFmtPagerAdapter(getSupportFragmentManager()));
        mBottomLayout.setAdapter(new HomeTabAdapter(), mViewPager);
    }


    private class HomeTabAdapter extends TabAdapter<View> {

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public View getView(int position, ViewGroup parent) {
            View tabView = getLayoutInflater().inflate(R.layout.item_tab_home, parent, false);
            ImageView icon = tabView.findViewById(R.id.tab_icon);
            // 中间位置特殊处理
            if (position == getCount() / 2) {
                int wh = DensityUtils.dp2px(getAppActivity(), 40);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(wh, wh);
                lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
                icon.setLayoutParams(lp);
            }
            icon.setImageResource(mTabIcons[position]);
            TextView text = tabView.findViewById(R.id.tab_text);
            text.setText(mTabTitles[position]);
            return tabView;
        }

        @Override
        public void onTabReset(View view, int position) {
            ImageView icon = view.findViewById(R.id.tab_icon);
            TextView text = view.findViewById(R.id.tab_text);
            icon.setSelected(false);
            text.setSelected(false);
        }

        @Override
        public void onTabSelected(View view, int position) {
            ImageView icon = view.findViewById(R.id.tab_icon);
            TextView text = view.findViewById(R.id.tab_text);
            icon.setSelected(true);
            text.setSelected(true);
        }
    }

    @Override
    protected void initTitle(DefTitleBar titleBar) {

    }

    @Override
    protected boolean isNeedTitle() {
        return false;
    }

}
